<?php

namespace app\model;

use think\Model;

class BaseModel extends Model {
    /**
     * 获取数据
     * @param int $limit
     * @param int $page
     * @param string $name
     * @param string $fields
     * @param string $order
     * @return array
     */
    public function _list(int $page, int $limit, string $name,string $fields = 'id', string $order = 'id ASC'): array {
        $paginate = '';
        if($name){
            $paginate = $this
                ->where('name','like','%'.$name.'%')
                ->field($fields)
                ->order($order)
                ->paginate([
                    'list_rows' => $limit,
                    'page'      => $page,
                ]);
        }else{
            $paginate = $this
                ->field($fields)
                ->order($order)
                ->paginate([
                    'list_rows' => $limit,
                    'page'      => $page,
                ]);
        }
        $data = $paginate->items();   // 当前页数据
        $total = $paginate->total();  // 总记录数

        if ($data && $total > 0)
            return ['code' => 200, 'msg'  => '获取成功', 'data' => $data, 'total' => $total,];
        return ['code' => 400, 'msg'  => '暂无数据', 'data' => [], 'total' => 0,];
    }

    /**
     * 删除数据
     * @param int $id
     * @return array
    */
    public function _delete(int $id) :array{
        $query = $this->find($id);
        if(!$query)
            return ['code' => 404, 'msg' => '删除数据不存在'];
        if($id === 1)
            return ['code' => 400, 'msg' => '包含不允许删除的数据'];
        $query->delete();
        return ['code' => 200, 'msg' => '删除成功'];
    }

    /**
     * 批量删除
     * @param array $idArray
     * @return array
     */
    public function _allDelete(array $idArray) :array{
        if (empty($idArray))
            return ['code' => 400, 'msg' => '删除数据不能为空'];
        if (in_array(1, $idArray))
            return ['code' => 400, 'msg' => '包含不允许删除的数据'];
        $this->destroy($idArray);
        return ['code' => 200, 'msg' => '删除成功'];
    }

    /**
     * 添加数据
     * @param array $fields
     * @param array $values
     * @return array
    */
    public function _create(array $fields, array $values): array{
        $data = array_combine($fields, $values);
        if($this->where('name',$data['name'])->find()){
            $modelName = class_basename($this);
            $msg = match($modelName) {
                'User' => '用户名已存在',
                'Role' => '角色名已存在',
                default => '权限标识已存在',
            };
            return ['code' => 400, 'msg' => $msg];
        }
        $query = $this->create($data);
        if($query)
            return ['code' => 200, 'msg' => '添加成功', 'id' => $query->id];
        return ['code' => 400, 'msg' => '添加失败'];
    }

    /**
     * 更新数据
     * @param array $fields
     * @param array $values
     * @return array
    */
    public function _update(array $fields, array $values): array{
        $data = array_combine($fields, $values);
        $query = $this->find($data['id']);
        if(!$query)
            return ['code' => 404, 'msg' => '用户不存在'];
        if($query->save($data))
            return ['code' => 200, 'msg' => '更新成功'];
        else
            return ['code' => 400, 'msg' => '更新失败'];
    }

    public function getStatusAttr($value): string{
        $status = [
            0 => '禁用',
            1 => '正常'
        ];
        return $status[$value];
    }
}